package cn.calm.osgi.admin.bundle.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class FileUtils {
	private static byte[] buffer = new byte[1024 * 5];

	public static void copyFile(File srcFile, File destFile) throws IOException {
		if (srcFile == null) {
			throw new NullPointerException("Source must not be null");
		}
		if (destFile == null) {
			throw new NullPointerException("Destination must not be null");
		}
		if (srcFile.exists() == false) {
			throw new FileNotFoundException("Source '" + srcFile
					+ "' does not exist");
		}
		if (srcFile.isDirectory()) {
			throw new IOException("Source '" + srcFile
					+ "' exists but is a directory");
		}
		if (srcFile.getCanonicalPath().equals(destFile.getCanonicalPath())) {
			throw new IOException("Source '" + srcFile + "' and destination '"
					+ destFile + "' are the same");
		}
		if (destFile.getParentFile() != null
				&& destFile.getParentFile().exists() == false) {
			if (destFile.getParentFile().mkdirs() == false) {
				throw new IOException("Destination '" + destFile
						+ "' directory cannot be created");
			}
		}
		if (destFile.exists() && destFile.canWrite() == false) {
			throw new IOException("Destination '" + destFile
					+ "' exists but is read-only");
		}
		FileInputStream fis = null;
		fis = new FileInputStream(srcFile);
		inputStreamToFile(fis, destFile);
	}

	public static void byteToFile(byte[] byteArry, File dist) {
		ByteArrayInputStream bais=new ByteArrayInputStream(byteArry);
		inputStreamToFile(bais, dist);
	}

	public static void inputStreamToFile(InputStream is, File dist) {
		if(!dist.getParentFile().isDirectory()){
			dist.getParentFile().mkdirs();
		}
		FileOutputStream fos = null;
		int length = 0;
		try {

			fos = new FileOutputStream(dist);
			while ((length = is.read(buffer)) != -1) {
				fos.write(buffer, 0, length);
			}
			fos.flush();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (fos != null) {
				try {
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (is != null) {
				try {
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

	}

	public static void byteToFile(byte[] byteArry, String string) {
		byteToFile(byteArry, new File(string));
	}

	public static byte[] fileToBytes(File bundleFile) throws IOException {
		
		FileInputStream fis=new FileInputStream(bundleFile);
		ByteArrayOutputStream baos=new ByteArrayOutputStream();
		
		int i=0;
		while((i=fis.read(buffer))!=-1){
			baos.write(buffer, 0, i);
		}
		try{
			byte[] buf=baos.toByteArray();
			return buf;
		}finally{
			if(baos!=null){
				baos.close();
			}
			if(fis!=null){
				fis.close();
			}
		}
		
		
	}
}
